'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    const now = new Date();
    
    // 先删除已存在的配置（强制替换）
    await queryInterface.bulkDelete('table_configs', {
      code: 'departments'
    }, {});
    
    await queryInterface.bulkInsert('table_configs', [{
      id: Sequelize.literal('UUID()'),
      code: 'departments',
      name: '部门管理',
      description: '组织架构部门管理',
      table_name: 'departments',
      api_path: '/departments',

      fields: JSON.stringify([
        {
          key: 'id',
          label: 'ID',
          type: 'text',
          table: { hidden: true },
          search: { searchable: false },
          form: { creatable: false, editable: false }
        },
        {
          key: 'name',
          label: '部门名称',
          type: 'text',
          table: { width: 200, align: 'left', sortable: true },
          search: { searchable: true, searchType: 'like', quickSearch: true },
          form: {
            creatable: true,
            editable: true,
            required: true,
            rules: [{ required: true, message: '请输入部门名称' }],
            placeholder: '请输入部门名称'
          }
        },
        {
          key: 'description',
          label: '部门描述',
          type: 'textarea',
          table: { width: 250, align: 'left', ellipsis: true },
          search: { searchable: false },
          form: { creatable: true, editable: true, placeholder: '请输入部门描述' }
        },
        {
          key: 'companyId',
          label: '所属公司',
          type: 'select',
          table: {
            width: 150,
            align: 'left',
            render: 'relation',
            relationField: 'company.name'
          },
          search: { searchable: true, searchType: 'equal' },
          form: {
            creatable: true,
            editable: true,
            required: true,
            rules: [{ required: true, message: '请选择所属公司' }],
            placeholder: '请选择所属公司',
            optionsSource: 'api',
            optionsApi: '/companies',
            optionsLabelField: 'name',
            optionsValueField: 'id'
          }
        },
        {
          key: 'parentId',
          label: '上级部门',
          type: 'select',
          table: {
            width: 150,
            align: 'left',
            render: 'relation',
            relationField: 'parent.name'
          },
          search: { searchable: false },
          form: {
            creatable: true,
            editable: true,
            placeholder: '请选择上级部门（可选）',
            optionsSource: 'api',
            optionsApi: '/departments',
            optionsLabelField: 'name',
            optionsValueField: 'id'
          }
        },
        {
          key: 'createdAt',
          label: '创建时间',
          type: 'datetime',
          table: { width: 180, sortable: true, format: 'YYYY-MM-DD HH:mm:ss' },
          search: { searchable: false },
          form: { creatable: false, editable: false }
        }
      ]),

      actions: JSON.stringify({
        create: true,
        update: true,
        delete: true,
        batchDelete: true,
        export: true
      }),

      table_props: JSON.stringify({
        size: 'middle',
        rowKey: 'id',
        pagination: { pageSize: 10, showSizeChanger: true },
        rowSelection: true
      }),

      permissions: JSON.stringify({
        view: 'department:read',
        create: 'department:create',
        update: 'department:update',
        delete: 'department:delete'
      }),

      status: true,
      created_at: now,
      updated_at: now
    }], {});
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('table_configs', { code: 'departments' }, {});
  }
};

